package bom.example.common.system.controller;

import bom.example.common.common.Results;
import bom.example.common.system.domain.SysLog;
import bom.example.common.system.service.SysLogService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author X.I.O
 * @title: LogController
 * @projectName springboot
 * @description: TODO
 * @date 2021/10/30 3:02
 * 日志服务模块
 */
@Api(tags = {"日志模块"})
@RestController
@RequestMapping("log")
public class LogController {

    @Autowired
    private SysLogService sysLogService;

    @ApiOperation("日志列表")
    @PostMapping("list")
    public JSONObject getLogListCOntroller(@ApiParam("页码") @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                           @ApiParam("条数") @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                           @ApiParam("关键字") @RequestParam(value = "keyword",required = false)String keyword){
        LambdaQueryWrapper<SysLog> sysLogLambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(keyword)){
            sysLogLambdaQueryWrapper.or().like(SysLog::getLogMethod,keyword);
            sysLogLambdaQueryWrapper.or().like(SysLog::getLogName,keyword);
            sysLogLambdaQueryWrapper.or().like(SysLog::getLogPath,keyword);
            sysLogLambdaQueryWrapper.or().like(SysLog::getLogUsername,keyword);
            sysLogLambdaQueryWrapper.or().like(SysLog::getLogRealname,keyword);
        }
        sysLogLambdaQueryWrapper.orderByDesc(SysLog::getLogCreatetime);
        Page page=new Page(pageNum,pageSize);
        IPage<SysLog> sysLogIPage=sysLogService.page(page,sysLogLambdaQueryWrapper);
        return Results.success(sysLogIPage.getRecords(),sysLogIPage.getTotal());
    }
}
